using System.IO;
using System.Xml.Serialization;

namespace MathematicallySafe.HeadCounter.Data.Common
{
    internal static class SerialisationHelper
    {
        public static T Load<T>(string filename)
        {
            if (!File.Exists(filename)) throw new FileNotFoundException("Could not find options file " + filename);

            T deserialize;

            var xmlSerializer = new XmlSerializer(typeof(T));
            using (Stream s = File.OpenRead(filename))
            {
                deserialize = (T)xmlSerializer.Deserialize(s);
            }

            return deserialize;
        }

        public static void Save<T>(string filename, T data)
        {
            var xmlSerializer = new XmlSerializer(typeof(T));

            using (Stream s = File.Create(filename))
            {
                xmlSerializer.Serialize(s, data);
            }
        }
    }
}